import axios from 'axios'
// import Vue from 'vue'
// import VueRouter from 'vue-router'
import { getItem } from '@/utils/storage.js'

// import jsonBig from 'json-bigint'

// import store from '@/store'
// axios.create 方法：复制一个 axios
const request = axios.create({
  baseURL: '' // 基础路径
})
// 请求拦截器
request.interceptors.request.use(

  function (config) {
    const token = getItem('sqtoken')

    config.headers.Authorization = `Bearer ${token}`
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'
// 响应拦截器
request.interceptors.response.use(
  response => {
    const { data } = response
    if (data.code === 401) {
      // 清除token
      localStorage.removeItem('token')
      alert('登录权限已过期，请从新登录')
    }
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

export default request
